﻿using Cloud.Core.Data;
using Cloud.Dto;
using Cloud.Dto.Sys;
using Cloud.Entity;
using Cloud.UnitOfWork;
using Cloud.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cloud.Service.Account
{
    public class AccountService : IAccountService
    {
        //private readonly IRepository<Sys_User> _userRepository;
        //private readonly IRepository<Sys_Module> _moduleRepository;
        //private readonly IRepository<Sys_Role_User> _roleUserRepository;
        //private readonly IRepository<Sys_Permission> _permissionRepository;
        //public AccountService(
        //    IRepository<Sys_User> _userRepository,
        //    IRepository<Sys_Module> _moduleRepository, 
        //    IRepository<Sys_Role_User> _roleUserRepository,
        //    IRepository<Sys_Permission> _permissionRepository
        //    )
        //{
        //    this._userRepository = _userRepository;
        //    this._moduleRepository = _moduleRepository;
        //    this._roleUserRepository = _roleUserRepository;
        //    this._permissionRepository = _permissionRepository;
        //}
        private readonly IUnitOfWork _unitOfWork;
        public AccountService(IUnitOfWork unitOfWork)
        {
            this._unitOfWork = unitOfWork;
        }
        public ServiceReturnDto<SysUserDto> Login(string name, string password)
        {
            ServiceReturnDto<SysUserDto> sr = new ServiceReturnDto<SysUserDto>() { IsSucceed = true };
            var user = _unitOfWork.GetRepository<Sys_User>().Entities.Where(c => c.IsDeleted == false && c.IsDimission == false && c.LoginName == name).FirstOrDefault();
            if (user == null)
            {
                sr.IsSucceed = false;
                sr.Message = "账号不存在";
                return sr;
            }
            string pwd = CryptHelper.DecryptDES(user.LoginPwd, user.SaltKey);
            if (password != pwd)
            {
                sr.IsSucceed = false;
                sr.Message = "密码错误";
                return sr;
            }
            sr.Data = new SysUserDto()
            {
                LoginID = user.Id,
                IsAdmin = user.IsAdmin,
                LoginName = user.LoginName,
                RelName = user.RelName
            };
            return sr;
        }
        public ServiceReturnDto<List<SysPermissionDto>> GetCurrentUserPermission(int loginId,bool isAdmin)
        {
            ServiceReturnDto<List<SysPermissionDto>> sr = new ServiceReturnDto<List<SysPermissionDto>>() { IsSucceed = true };
            if (isAdmin)
            {
                sr.Data= _unitOfWork.GetRepository<Sys_Module>().Entities.Where(c => c.IsDeleted == false).OrderBy(c => c.Sort).Select(c =>
          new SysPermissionDto() { id = c.Id, name = c.Name, controller = c.ControllerName, action = c.ActionName, parentId = c.ParentID, url =string.Concat( "/" , c.ControllerName , "/" , c.ActionName) }).ToList();

            }
            else
            {
                var roleList = _unitOfWork.GetRepository<Sys_Role_User>().Entities.Where(c => c.IsDeleted == false && c.UserID == loginId).Select(c => c.RoleID).ToList();
                var moduleList = _unitOfWork.GetRepository<Sys_Permission>().Entities.Where(c => roleList.Contains(c.RoleID)).Select(c => c.ModuleID).Distinct().ToList();
                sr.Data = _unitOfWork.GetRepository<Sys_Module>().Entities.Where(c => c.IsDeleted == false && moduleList.Contains(c.Id)).OrderBy(c => c.Sort).Select(c =>
         new SysPermissionDto() { id = c.Id, name = c.Name, controller = c.ControllerName , action = c.ActionName, parentId = c.ParentID, url = string.Concat("/", c.ControllerName, "/", c.ActionName) }).ToList();
            }
            return sr;
        }


        public async Task<ServiceReturnDto> ModifyPassword(ModifyPasswordDto model)
        {
            ServiceReturnDto sr = new ServiceReturnDto() {
                 
            };
            if (string.IsNullOrEmpty(model.OldPwd))
            {
                sr.Message = "请输入原密码";
                return sr;
            }
            if (string.IsNullOrEmpty(model.NewPwd))
            {
                sr.Message = "请输入新密码";
                return sr;
            }
            if (string.IsNullOrEmpty(model.NewPwd1))
            {
                sr.Message = "请输入重复新密码";
                return sr;
            }
            if (model.NewPwd!=model.NewPwd1)
            {
                sr.Message = "两次输入的新密码不一致";
                return sr;
            }
            var account=await _unitOfWork.GetRepository<Sys_User>().GetAsync(c => c.IsDeleted == false && c.Id == model.LoginId);
            if (account==null)
            {
                sr.Message = "要修改的用户不存在";
                return sr;
            }
            var pwd = CryptHelper.DecryptDES(account.LoginPwd, account.SaltKey);
            if (pwd!=model.OldPwd)
            {
                sr.Message = "旧密码输入错误";
                return sr;
            }
            account.LoginPwd = CryptHelper.EncryptDES(model.NewPwd, account.SaltKey);
            var updatedPropertyList= PropertyExpression<Sys_User>.Init.Property(c => c.LoginPwd).ToList();
            sr.IsSucceed = true;
            await _unitOfWork.GetRepository<Sys_User>().UpdateAsync(account, true, updatedPropertyList);
            return sr;
        }

    }
}
